package com.ai.ly.goodsautocat.autocat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ai.ly.goodsautocat.common.Constants;
import com.ai.ly.goodsautocat.util.FileUtil;
import com.ai.ly.goodsautocat.util.JiebaAnalysisUtil;
import com.ai.ly.goodsautocat.util.ReadWriteFile;
import com.ai.ly.goodsautocat.util.WordFilterFactory;
import com.huaban.analysis.jieba.SegToken;

/**
 * 
 * Title: 把样本进行分词，生成一个词典 <br>
 * Description: <br>
 * Date: 2018年3月29日 <br>
 * Copyright (c) 2018 AILK <br>
 * 
 * @author liangyi
 */
public class SplitSrcWordsToDict {
    
    static final int WORD_COUNT_FLAG=5;//如果某个词出现的数量小于该数字，就不要记录到字典里了
    
    static final int MAX_WORD_COUNT=Integer.MAX_VALUE;//只保留这么多个字典

    public static void main(String[] args) throws Exception {
        buildDict();
    }
    
    public static void buildDict() throws Exception{

        
        Map<String, Integer> wordMap=new HashMap<>();
        
        File file=FileUtil.getFileInSys(Constants.orgSampleFilePath);
        
        //读取每一行数据
        try {
            BufferedReader e = new BufferedReader(new FileReader(file));
            for (String lineContent = e.readLine(); lineContent != null; lineContent = e.readLine()) {
                //解析每行数据
                int lastIndex=lineContent.lastIndexOf(",");
                if (lastIndex<=0) {
                    continue;
                }
                String gdsName=lineContent.substring(0, lastIndex);
                //对GdsName进行分词
                List<SegToken> wordList = JiebaAnalysisUtil.processInSearchMode(gdsName);
                addWordListToMap(wordList,wordMap);
            }
            e.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        //最后对wordMap进行排序
        List<WordInfo> wordInfoList=new ArrayList<>(wordMap.size());
        for (Map.Entry<String, Integer> entry : wordMap.entrySet()) {
            WordInfo wordInfo=new WordInfo();
            wordInfo.setCount(entry.getValue());
            wordInfo.setWord(entry.getKey());
            wordInfoList.add(wordInfo);
        }
        Collections.sort(wordInfoList);
        
        //写入新文件
        File dictFile=FileUtil.getFileInSys(AutoCatConstant.dictFilePath);
        if (dictFile.exists()) {
            dictFile.delete();
        }
        dictFile.createNewFile();
        String lineSeparator= Constants.lineSeparator;
        
        int wordCount=0;
        StringBuilder fileContent=new StringBuilder();
        for (WordInfo wordInfo : wordInfoList) {
            if (wordInfo.getCount()<WORD_COUNT_FLAG) {
                break;
            }
            String newLine=wordInfo.getWord()+" "+wordInfo.getCount()+lineSeparator;
            fileContent.append(newLine);
            
            wordCount++;
            if (wordCount>=MAX_WORD_COUNT) {
                break;
            }
        }
        ReadWriteFile.write(dictFile,StringUtils.trim(fileContent.toString()), false);
    
    }
    
    private static void addWordListToMap(List<SegToken> wordList,Map<String, Integer> wordMap) {
        if (wordList==null||wordList.size()<=0) {
            return;
        }
        for (SegToken segToken : wordList) {
            String word=segToken.word;
            if (WordFilterFactory.shouldFilterWord(word)) {
                continue;
            }
            
            Integer count=wordMap.get(word);
            if (count==null) {
                count=1;
            }
            count++;
            wordMap.put(word, count);
        }
    }
    
    public static class WordInfo implements Comparable<WordInfo>{
        private String word;
        private int count;
        public String getWord() {
            return word;
        }
        public void setWord(String word) {
            this.word = word;
        }
        public int getCount() {
            return count;
        }
        public void setCount(int count) {
            this.count = count;
        }
        @Override
        public int compareTo(WordInfo o) {
            if (o.getCount()>this.getCount()) {
                return 1;
            }else if (o.getCount()==this.getCount()) {
                return 0;
            }
            return -1;
        }
    }
    
}
